package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceCategoryMapper;
import com.zhijian.medical.entity.po.DeviceCategory;
import com.zhijian.medical.entity.vo.request.DeviceCategorySearchVo;
import com.zhijian.medical.entity.vo.response.DeviceCategoryVo;
import com.zhijian.medical.service.DeviceCategoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备品类业务处理层
 *
 * @author HCG
 * @date 2022-01-13 15:44:03
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceCategoryServiceImpl implements DeviceCategoryService {

    @Autowired
    private DeviceCategoryMapper mapper;

    @Override
    public DeviceCategory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceCategoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceCategory deviceCategory) {
        Date now = new Date();
        deviceCategory.setCreateTime(now);
        deviceCategory.setUpdateTime(now);
        return mapper.insert(deviceCategory) == 1 ? deviceCategory.getId() : 0L;
    }

    @Override
    public boolean update(DeviceCategory deviceCategory) {
        Date now = new Date();
        deviceCategory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceCategory) != 0;
    }

    @Override
    public Integer count(DeviceCategorySearchVo deviceCategorySearchVo) {
        return mapper.count(deviceCategorySearchVo);
    }

    @Override
    public List<DeviceCategoryVo> selectList(DeviceCategorySearchVo deviceCategorySearchVo) {
        return mapper.selectList(deviceCategorySearchVo);
    }

    @Override
    public Page<DeviceCategoryVo> selectPage(DeviceCategorySearchVo deviceCategorySearchVo) {
        //先直接查总数
        int count = count(deviceCategorySearchVo);
        //分页
        Page<DeviceCategoryVo> page = new Page<>(true, count, deviceCategorySearchVo.getPageNum(), deviceCategorySearchVo.getPageSize());
        deviceCategorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceCategoryVo> responseVos = this.selectList(deviceCategorySearchVo);
        page.setData(responseVos);
        return page;
    }

}